package com.gitee.elead.model.serializer;

import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

import org.apache.commons.lang3.StringUtils;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import cn.hutool.core.date.DatePattern;

/**
 * 接受字符串类型的时间转成LocalDate
 * @author luopeng
 *
 */
public class DateJsonDeserializer extends JsonDeserializer<LocalDate> {

	@Override
	public LocalDate deserialize(JsonParser jsonParser, DeserializationContext ctxt) throws IOException, JsonProcessingException {
		try {
			if (ObjectUtils.isNotEmpty(jsonParser) && StringUtils.isNotEmpty(jsonParser.getText())) {
				return LocalDate.parse(jsonParser.getText(), DateTimeFormatter.ofPattern(DatePattern.NORM_DATE_PATTERN));
			} else {
				return null;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

}
